package com.beauty.dressing.module.market.fragments;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.LeaseItemDetail;
import com.beauty.dressing.common.bean.SaleItemDetail;
import com.beauty.dressing.common.bean.SalesOrderResp;
import com.beauty.dressing.common.bean.ShopCarBean;
import com.beauty.dressing.common.bean.StringBase;
import com.beauty.dressing.common.utils.Constants;
import com.beauty.dressing.common.view.LoadingNodataLayout;
import com.beauty.dressing.module.market.adapter.ShopDetailAdapter;
import com.beauty.dressing.module.market.holder.ShopHeaderHolder;
import com.beauty.dressing.module.market.presenter.LeaseDetailPresenter;
import com.beauty.dressing.module.market.presenter.SaleDetailPresenter;
import com.beauty.dressing.module.market.presenter.contract.SaleDetailContract;
import com.beauty.dressing.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/5/20
 */

public class GoodsFragment extends BaseFragment implements SaleDetailContract.ViewControl {
    private ShopDetailAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private SaleDetailPresenter mPresenter = new SaleDetailPresenter(this);
    private long sales_id;
    private ShopHeaderHolder mHeaderHolder;
    private LoadingNodataLayout mNodtaLayout;
    private SaleItemDetail saleItemDetail;
    @Override
    public void setContentView() {
        setContentView(R.layout.common_recycler_no_title);
        Bundle args = getArguments();
        if (args != null) {
            sales_id = args.getLong(Constants.SALES_ID,0);
        }
    }

    public boolean hasSaleDetail(){
        return saleItemDetail != null;
    }

    public ShopCarBean createShopCar(){
        if (saleItemDetail ==null) return null;

        ShopCarBean shopCarBean = new ShopCarBean(0, saleItemDetail.sales_id, saleItemDetail.sales_name,
                saleItemDetail.image, saleItemDetail.getPrice(),0, 1,Constants.SHOP_CAR_SALE,0);
        return shopCarBean;
    }

    @Override
    public void findView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mNodtaLayout = (LoadingNodataLayout) findViewById(R.id.nodata_layout);
        mNodtaLayout.setCenterGravity();
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new ShopDetailAdapter(context);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initData() {
        mHeaderHolder = new ShopHeaderHolder(context, LayoutInflater.from(context),mRecyclerView);
        mAdapter.addHeaderView(mHeaderHolder.itemView);
        mNodtaLayout.setVisibility(View.VISIBLE);
        mNodtaLayout.setIsLoadingUi(true);
        mPresenter.getSalesDetail(sales_id);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.clear();
    }


    @Override
    public void onSaleDetail(boolean isSuccess, int code, String message, SaleItemDetail data) {
        if (isSuccess){
            if (data != null){
                saleItemDetail = data;
                mHeaderHolder.bindData(data);
                if (data.img != null && !data.img.isEmpty()){
                    List<BaseBean> beanList = new ArrayList<>();
                    for (String str:data.img){
                        beanList.add(new StringBase(str));
                    }
                    mAdapter.setChild(beanList);
                }
                mNodtaLayout.setVisibility(View.GONE);
            } else {
                mNodtaLayout.setIsLoadingUi(false);
            }
        } else {
            mNodtaLayout.setIsLoadingUi(false);
        }
    }

    @Override
    public void onSaleAddCart(boolean isSuccess, int code, String message, Object data) {

    }

    @Override
    public void onCreateSaleOrder(boolean isSuccess, int code, String message, SalesOrderResp data) {

    }
}
